package cn.maihe.elg.operation.centers.quanzhou.config;

import cn.maihe.elg.operation.centers.quanzhou.auth.QuanzhouAuthorization;
import cn.maihe.elg.operation.centers.quanzhou.auth.QuanzhouCenterAuthConfig;
import cn.maihe.elg.operation.centers.quanzhou.auth.QuanzhouEncryption;
import cn.maihe.elg.operation.config.ElgAuthConfig;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 泉州中心配置文件
 * @Author xxx
 * @Date 2025/08/31
 */
@Configuration
@ConfigurationProperties(prefix = "cn.maihe.elg.center-info-config.quanzhou")
@Getter
@Setter
@Slf4j
public class QuanzhouInfoConfig {

    /**
     * key=CenterNoEnum.name.toLowerCase yml自动加载
     */
    private Map<String, CenterInfo> quanzhouCenterMap = new HashMap<>();

    @JsonIgnore
    private final static Map<String, QuanzhouCenterAuthConfig> authConfigMap = new HashMap<>();

    @Resource
    private ElgAuthConfig elgAuthConfig;

    private CenterNoEnum getByCenterNoEnum(String centerNoEnumName) {
        CenterNoEnum centerNoEnum = Arrays.stream(CenterNoEnum.values())
                .filter(o -> o.name().toLowerCase().equalsIgnoreCase(centerNoEnumName))
                .findFirst().orElse(null);
        Assert.notNull(centerNoEnum, "非法中心枚举名称[" + centerNoEnumName + "]");
        return centerNoEnum;
    }

    /**
     * 获取中心认证配置
     */
    public QuanzhouCenterAuthConfig getAuthConfig(CenterNoEnum centerNoEnum) {
        String key = centerNoEnum.name().toLowerCase();
        if (authConfigMap.containsKey(key)) {
            return authConfigMap.get(key);
        }

        CenterInfo centerInfo = quanzhouCenterMap.get(key);
        Assert.notNull(centerInfo, "泉州中心[" + centerNoEnum.getCenterName() + "]配置信息不存在");

        QuanzhouAuthorization authorization = new QuanzhouAuthorization(
                centerInfo.getAppkey(),
                centerInfo.getAppsecret(),
                centerInfo.getPlatformcode(),
                centerInfo.getProductcode()
        );

        QuanzhouEncryption encryption = new QuanzhouEncryption(
                centerInfo.getSm4Key(),
                centerInfo.getSm4Iv()
        );

        // 创建泉州中心认证配置对象，用于初始化认证相关的配置信息
        // 该配置包含授权信息、加密配置以及中心节点信息
        QuanzhouCenterAuthConfig authConfig = new QuanzhouCenterAuthConfig(
                authorization, encryption, centerInfo
        );


        authConfigMap.put(key, authConfig);
        return authConfig;
    }

    @Getter
    @Setter
    @Validated
    public static class CenterInfo implements Serializable {

        /**
         * 应用密钥
         */
        @NotEmpty(message = "appkey不能为空")
        private String appkey;

        /**
         * 应用秘钥
         */
        @NotEmpty(message = "appsecret不能为空")
        private String appsecret;

        /**
         * 平台编码
         */
        @NotEmpty(message = "platformcode不能为空")
        private String platformcode;

        /**
         * 产品编号
         */
        @NotEmpty(message = "productcode不能为空")
        private String productcode;

        /**
         * SM4加密密钥
         */
        @NotEmpty(message = "sm4Key不能为空")
        private String sm4Key;

        /**
         * SM4加密向量
         */
        @NotEmpty(message = "sm4Iv不能为空")
        private String sm4Iv;

        /**
         * 系统名称
         */
        @NotEmpty(message = "中心系统名称不能为空")
        private String sysName;

        /**
         * 请求平台接口地址前缀
         */
        @NotEmpty(message = "中心url不能为空")
        private String url;

        /**
         * 投保申请结果通知地址
         */
        private String applyResultNoticeUrl;

        /**
         * 支付结果通知地址
         */
        private String payResultNoticeUrl;

        /**
         * 退保通知接口地址
         */
        private String surrenderNoticeUrl;

        /**
         * 理赔结果通知接口地址
         */
        private String claimResultNoticeUrl;
    }
}